package com.naiterui.ehp.bs.doctor.interceptor;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.naiterui.ehp.bp.support.exception.BaseExceptionCodes;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.security.DoctorLoginInterceptor;
import com.naiterui.ehp.bp.support.security.HeaderConstant;
import com.naiterui.ehp.bp.support.security.SettingConfig;
import com.naiterui.ehp.bp.support.utils.JsonMapper;
import com.naiterui.ehp.bp.utils.context.SpringContextHolder;
import com.naiterui.ehp.bs.doctor.fegin.IPcFeignClient;
import com.naiterui.ehp.bs.doctor.service.security.ISecurityService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class LoginInterceptor extends DoctorLoginInterceptor {

    @Override
    protected boolean verifyToken(HttpServletRequest request, HttpServletResponse response) throws
            NumberFormatException,
            IOException {

                boolean result = false;
                String origin = request.getHeader(HeaderConstant.HEADER_ORIGIN);
        String doctorId = request.getParameter(SettingConfig.DOCTOR_ID);

        String token = request.getParameter(SettingConfig.TOKEN);
        // 如果是医生pc端和小程序端的token 是通过header头传递的
        String tokenHeader = request.getHeader(SettingConfig.TOKEN_HEADER);

        if (StringUtils.isNumeric(origin)) {
            switch (Integer.parseInt(origin)) {
                case HeaderConstant.HEADER_ORIGIN_DOCTOR_APP:
                    ISecurityService securityService =
                        SpringContextHolder.getBean("adSecurityServiceImpl", ISecurityService.class);
                    result = securityService.saveCheckToken(doctorId, token, response);
                    break;

                case HeaderConstant.HEADER_ORIGIN_DOCTOR_PC:
                    Long tokenUserId = this.checkDoctorPcToken(tokenHeader);
                    if (doctorId != null && doctorId.equals(String.valueOf(tokenUserId))) {
                        result = true;
                    }
                    break;

                default:
                    result = false;
                    break;
            }
        }

        if (!result) {
            response.getWriter().println(JsonMapper.toJson(PublicService.returnResponseVO(BaseExceptionCodes.TOLOGIN)));
        }

        return result;
    }

    private Long checkDoctorPcToken(String token) {
        if (StringUtils.isBlank(token)) {
            return null;
        }
        IPcFeignClient pcFeignClient = SpringContextHolder.getBean(IPcFeignClient.class);
        try {
            Long id = pcFeignClient.checkDoctorPcToken(token);
            log.info("checkDoctorPcToken id:{}", id);
            return id;
        } catch (BusinessException e) {
            log.error("校验医生pc端token失败", e);
        }
        return null;
    }
}
